package com.alog.yzd.bookmall.service.catagory;

import com.alog.yzd.bookmall.dao.catagory.CatagoryDao;
import com.alog.yzd.bookmall.domain.catagory.Catagory;

import java.sql.SQLException;
import java.util.List;

/**
 * 分类模块的业务层
 */
public class CatagoryService {
    private CatagoryDao catagoryDao = new CatagoryDao();

    /**
     * 查询父分类下的所有子分类
     */
    public List<Catagory> findChildren(String pid){
        try {
            return catagoryDao.findByParent(pid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除分类
     */
    public void delete(String cid){
        try {
            catagoryDao.delete(cid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 查找子分类的个数
     */
    public int findChildCountbyParent(String pid){
        try {
            return catagoryDao.findChildCountByParent(pid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改分类
     */
    public void edit(Catagory catagory){
        try {
            catagoryDao.edit(catagory);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 加载Catagory
     */
    public Catagory load(String cid){
        try {
            return catagoryDao.load(cid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询所有的一级分类
     */
    public List<Catagory> findParents(){
        try {
            return catagoryDao.findParents();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 添加分类
     */
    public void add(Catagory catagory){
        try {
            catagoryDao.add(catagory);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取所有分类
     * @return
     */
    public List<Catagory> findAll(){
        try {
            return catagoryDao.findAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
